package guoanmaker.operator.business.dao;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import guoanmaker.operator.business.modelnobind.OperatorContractDemandBind;

/**
 * 
* @ClassName: OperatorContractDemandDao 
* @Description: TODO(合同需求操作dao) 
* @author (ZhangXinYu)  
* @date 2017年12月17日 下午4:34:24 
* @version V1.0
 */
@Repository
public class OperatorContractDemandDao {
	@Autowired
	private EntityManager entityManager;
		
	@SuppressWarnings("unchecked")
	public List<OperatorContractDemandBind> findContractDemand(String name , String startDate , String endDate, Integer currentPage , Integer pageSize){
		List<OperatorContractDemandBind> contractDemandBinds = null;
		String jpql = "select new guoanmaker.operator.business.modelnobind.OperatorContractDemandBind(d.id, d.type, d.name, d.email, d.date) from OperatorContractDemand d where 1=1";
		if(name != null && !name.equals("")){
			jpql += " and d.name like '%"+name+"%'";
		}
		/*if((startDate != null && !startDate.equals("")) && (endDate != null && !endDate.equals(""))){
			jpql += " and d.date between '"+startDate+"' and '"+endDate+"'";
		}*/
		if(startDate != null && !startDate.equals("")){
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
			try {
				Date d = format.parse(startDate.replace("Z", " UTC"));
				format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				//format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");//注意格式化的表达式
				startDate = format.format(d);
			} catch (ParseException e) {
				e.printStackTrace();
			}//注意是空格+UTC
			jpql += " and d.date > '"+startDate+"'";
		}
		if(endDate != null && !endDate.equals("")){
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
			try {
				Date d = format.parse(endDate.replace("Z", " UTC"));
				format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				//format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");//注意格式化的表达式
				//format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				//设置结束如期为当日23:59:59
				//format = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
				endDate = format.format(d);
			} catch (ParseException e) {
				e.printStackTrace();
			}//注意是空格+UTC
			jpql +=" and d.date < '"+endDate+"'";
		}
		jpql += " order by d.date desc";
		Query query = entityManager.createQuery(jpql).setFirstResult(currentPage*pageSize).setMaxResults(pageSize);
		contractDemandBinds = query.getResultList();
		return contractDemandBinds;
	}
	
	@SuppressWarnings("unchecked")
	public Integer findContractDemandCount(String name , String startDate , String endDate){
		List<OperatorContractDemandBind> contractDemandBinds = null;
		String jpql = "select new guoanmaker.operator.business.modelnobind.OperatorContractDemandBind(d.id, d.type, d.name, d.email, d.date) from OperatorContractDemand d where 1=1";
		if(name != null && !name.equals("")){
			jpql += " and d.name like '%"+name+"%'";
		}
		/*if((startDate != null && !startDate.equals("")) && (endDate != null && !endDate.equals(""))){
			jpql += " and d.date between '"+startDate+"' and '"+endDate+"'";
		}*/
		if(startDate != null && !startDate.equals("")){
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
			try {
				Date d = format.parse(startDate.replace("Z", " UTC"));
				format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				//format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");//注意格式化的表达式
				startDate = format.format(d);
			} catch (ParseException e) {
				e.printStackTrace();
			}//注意是空格+UTC
			jpql += " and d.date > '"+startDate+"'";
		}
		if(endDate != null && !endDate.equals("")){
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
			try {
				Date d = format.parse(endDate.replace("Z", " UTC"));
				format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				//format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");//注意格式化的表达式
				//format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				//设置结束如期为当日23:59:59
				//format = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
				endDate = format.format(d);
			} catch (ParseException e) {
				e.printStackTrace();
			}//注意是空格+UTC
			jpql +=" and d.date < '"+endDate+"'";
		}
		jpql += " order by d.date desc";
		Query query = entityManager.createQuery(jpql);
		contractDemandBinds = query.getResultList();
		return contractDemandBinds.size();
	}
}
